'use strict';

angular.module('QC').controller('QCNormController',
    ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', 'BaseService', '$location', 'Authentication',
        function ($scope, $stateParams, PublicModule, PublicFunction, BaseService, $location, Authentication) {
            $scope.TypeEnum;
            BaseService.getEnum('QCEnum.QCNormType', function(enumData){
                $scope.TypeEnum = enumData;
            });

            $scope.btnSave = function () {
                if ($scope.Module.QCNorm.FocusedRecord.Code == undefined ||
                    $scope.Module.QCNorm.FocusedRecord.Code.trim() == "") {
                    PublicFunction.alert("编码不能为空！", "保存错误");
                    return;
                }
                if ($scope.Module.QCNorm.FocusedRecord.Name == undefined ||
                    $scope.Module.QCNorm.FocusedRecord.Name.trim() == "") {
                    PublicFunction.alert("名称不能为空！", "保存错误");
                    return;
                }
                if ($scope.Module.QCNorm.FocusedRecord.Type == undefined ||
                    $scope.Module.QCNorm.FocusedRecord.Type < 0) {
                    PublicFunction.alert("类型不能为空！", "保存错误");
                    return;
                }
                else{
                    if ($scope.Module.QCNorm.FocusedRecord.Type == 0){
                        if($scope.Module.QCNorm.FocusedRecord.Unit.trim() == ""){
                            PublicFunction.alert("类型为数值时，单位不能为空！", "保存错误");
                            return;
                        }
                    }
                    else if ($scope.Module.QCNorm.FocusedRecord.Type == 3){
                        if($scope.Module.QCNorm.FocusedRecord.ExtendEnum == undefined || 
                            $scope.Module.QCNorm.FocusedRecord.ExtendEnum == null){
                            PublicFunction.alert("类型为选择时，扩展枚举不能为空！", "保存错误");
                            return;
                        }
                    }
                }
                $scope.Module.QCNorm.save();
            };
            $scope.btnCancel = function () {
                $scope.Module.QCNorm.cancel();
            };
            $scope.btnAdd = function () {
                $scope.Module.QCNorm.add();
            };
            $scope.btnDelete = function () {
                $scope.Module.QCNorm.delete();
            };
            $scope.btnList = function () {
                $location.url('/index/QCNorm/List');
            };

            //初始化
            PublicModule.initModule($scope, 'QCNorm');
            $scope.Module.QCNorm.Populate = {path: 'ExtendEnum', select: 'Code Name'};

            if ($stateParams.id) {
                $scope.Module.QCNorm.findById($stateParams.id, function (record) {
                    $scope.Module.QCNorm.FocusedRecord = record;
                })
            }
            else {
                $scope.btnAdd();
            }
            ;
            $scope.Menu = '首页';
        }
    ]);